package com.dsg.controller.contact;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.contact.DsgContactLog;
import com.dsg.entity.dto.ContactLogDto;
import com.dsg.service.contact.IDsgContactLogService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * CONTACT LOG主表 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-10-06
 */
@RestController
@RequestMapping("/contact/dsgContactLog")
public class DsgContactLogController extends BaseController {
    @Autowired
    IDsgContactLogService service;

    @GetMapping(value = "/getLogList")
    @ApiOperation("非現有客戶接觸日誌")
    @ApiResponses({
            @ApiResponse(response = ContactLogDto.class, code = 1, message = "ok")
    })
    public AjaxResult getLogList() {
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, service.getLogList(getUserId()));
        return ajaxResult;
    }

    /**
     * 查询列表（分页）
     *
     * @param dsgContactLog
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgContactLog dsgContactLog) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("CREATE_TIME");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        dsgContactLog.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = service.selectByList(page, dsgContactLog);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 新增(整)
     *
     * @param dsgContactLog
     * @return
     */
    @NoRepeatSubmit
    @PostMapping(value = "/addWhole")
    @ApiOperation("新增(整)")
    public AjaxResult addWhole(@RequestBody DsgContactLog dsgContactLog) {
        boolean flag = service.addWhole(dsgContactLog);
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 更新(整)
     *
     * @param dsgContactLog
     * @return
     */
    @NoRepeatSubmit
    @PutMapping(value = "/updateWhole")
    @ApiOperation("更新(整)")
    public AjaxResult updateWhole(@RequestBody DsgContactLog dsgContactLog) {
        AjaxResult ajaxResult;
        boolean flag = service.updateWhole(dsgContactLog);
        if (flag) {
            ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgContactLog);
        } else {
            ajaxResult = result(MessageConstants.SSO_STATUS_FAIL);
        }
        return ajaxResult;
    }
}
